#include <buola/scene/cindexdata.h>
#include <buola/gl.h>

namespace buola { namespace scene {

CIndexData::CIndexData()
    :   mIBO(0)
    ,   mIndexSize(sizeof(GLuint))
    ,   mIndexCount(0)
    ,   mRestartIndex(16777216)
{

}

CIndexData::~CIndexData()
{
}

void CIndexData::Resize(int pCount)
{
    if(!mIBO)
    {
        glGenBuffers(1,&mIBO);
    }

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,pCount*mIndexSize,nullptr,GL_STATIC_DRAW);
    glPrimitiveRestartIndex(mRestartIndex);

    mIndexCount=pCount;
}

uint32_t *CIndexData::Map(GLenum pType)
{
    if(!mIBO) return nullptr;

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mIBO);
    return (uint32_t*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,pType);
}

void CIndexData::Unmap()
{
    glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}

void CIndexData::SetPrimitiveRestartIndex(GLuint pIndex)
{
    mRestartIndex=pIndex;
    if(mIBO)
        glPrimitiveRestartIndex(pIndex);
}

/*namespace gl*/ } /*namespace buola*/ }
